package cn.bone.ou.myuser.service.impl;

import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.Md5Util;
import cn.bone.base.utils.StringUtil;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.cache.RegionCache;
import cn.bone.ou.community.bean.Community;
import cn.bone.ou.community.bean.CommunityMng;
import cn.bone.ou.community.bean.SysRegion;
import cn.bone.ou.community.dto.CommunityDto;
import cn.bone.ou.community.mapper.CommunityMapper;
import cn.bone.ou.community.mapper.CommunityMngMapper;
import cn.bone.ou.community.mapper.SysRegionMapper;
import cn.bone.ou.myuser.param.MyParam;
import cn.bone.ou.myuser.service.IMyService;
import cn.bone.ou.myuser.vo.MyVo;
import cn.bone.ou.myuser.vo.RegionVo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class MyServiceImpl implements IMyService {

    @Resource
    private CommunityMapper communityMapper;
    @Resource
    private SysRegionMapper regionMapper;
    @Resource
    private CommunityMngMapper communityMngMapper;
    @Resource
    private RegionCache regionCache;

    /**获取列表数据*/
    public CommonResult<DataGrid<MyVo.ListVo>> list(MyParam.ListParam param){

        Date startDay = null;
        Date endDay = null;
        if(!StringUtil.isEmpty(param.getStartTime())){
            startDay = TimeUtil.parseYYYYMMdd(param.getStartTime());
        }
        if(!StringUtil.isEmpty(param.getEndTime())){
            endDay = TimeUtil.parseYYYYMMdd(param.getEndTime());
            endDay = DateUtils.addDays(endDay,1);
        }
        List<CommunityDto.ListDto> dtos = communityMapper.list(
                param.getStart(), param.getNum(),
                param.getName(), param.getRegionNo(),
                startDay, endDay,
                param.getUsable(),param.getMng());
        int count = communityMapper.countList(
                param.getName(),param.getRegionNo(),
                startDay,endDay,
                param.getUsable(),param.getMng());

        DataGrid<MyVo.ListVo> dataGrid = new DataGrid<>();
        List<MyVo.ListVo> vos = dtos.stream().map(dto->{
            MyVo.ListVo vo = new MyVo.ListVo();
            vo.setId(dto.getId());
            vo.setName(dto.getName());
            vo.setProvince(dto.getProvince());
            vo.setProvinceName(regionCache.get(dto.getProvince()).getName());
            vo.setCity(dto.getCity());
            vo.setCityName(regionCache.get(dto.getCity()).getName());
            vo.setDistrict(dto.getDistrict());
            vo.setDistrictName(regionCache.get(dto.getDistrict()).getName());
            vo.setAddress(dto.getAddress());
            vo.setUsable(dto.getUsable());
            vo.setHouseNum(dto.getHouseNum());
            vo.setBuildAreaNum(dto.getBuildAreaNum());
            vo.setExclusiveArea(dto.getExclusiveArea());
            vo.setCreateMemberId(dto.getCreateMemberId());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(dto.getCreateTime()));
            vo.setMngNum(dto.getMngNum());
            return vo;
        }).collect(Collectors.toList());
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**获取地区列表*/
    public CommonResult<List<RegionVo.ListVo>> regions(){
        List<SysRegion> list = regionCache.list();

        List<RegionVo.ListVo> vos = list.stream().map(region->{
            RegionVo.ListVo vo = new RegionVo.ListVo();
            vo.setName(region.getName());
            vo.setNo(region.getNo());
            vo.setLevel(region.getLevel());
            vo.setPno(region.getPno());
            return vo;
        }).collect(Collectors.toList());
        return Ref.ok(vos);
    }


    /**禁用启用*/
    public CommonResult<Void> usable(MyParam.UsableParam param){
        Community community = communityMapper.selectById(param.getId());
        if(community==null){
            return Ref.error("错误的小区编号");
        }
        community.setUsable(param.getUsable());
        communityMapper.updateById(community);
        return Ref.ok();
    }

    /**添加小区*/
    public CommonResult<Void> add(MyParam.AddParam param){

        String province = param.getRegionNo().substring(0,2)+"0000";
        String city = param.getRegionNo().substring(0,4)+"00";
        Date date = new Date();
        Community community = new Community();
        community.setName(param.getName());
        community.setProvince(province);
        community.setCity(city);
        community.setDistrict(param.getRegionNo());
        community.setAddress(param.getAddress());
        community.setHouseNum(param.getHouseNum());
        community.setBuildAreaNum(param.getBuildAreaNum());
        community.setExclusiveArea(param.getExclusiveArea());
        community.setCreateMemberId(1);
        community.setCreateTime(date);
        community.setUpdateMemberId(1);
        community.setUpdateTime(date);
        community.setUsable(1);
        communityMapper.insert(community);
        return Ref.ok();
    }

    /**修改小区*/
    public CommonResult<Void> modify(MyParam.ModifyParam param){
        Community community = communityMapper.selectById(param.getId());
        if(community==null){
            return Ref.error("错误的小区id");
        }
        String province = param.getRegionNo().substring(0,2)+"0000";
        String city = param.getRegionNo().substring(0,4)+"00";
        Date date = new Date();
        community.setName(param.getName());
        community.setProvince(province);
        community.setCity(city);
        community.setDistrict(param.getRegionNo());
        community.setAddress(param.getAddress());
        community.setHouseNum(param.getHouseNum());
        community.setBuildAreaNum(param.getBuildAreaNum());
        community.setExclusiveArea(param.getExclusiveArea());
        community.setUpdateMemberId(1);
        community.setUpdateTime(date);
        communityMapper.updateById(community);
        return Ref.ok();
    }

    /**获取小区管理员列表*/
    public CommonResult<DataGrid<MyVo.ListMngVo>> listMng(MyParam.ListMngParam param){

        List<CommunityMng> list = communityMngMapper.listByCommunityId(param.getCommunityId(),
                param.getStart(), param.getNum());
        int count = communityMngMapper.countListByCommunityId(param.getCommunityId());

        List<MyVo.ListMngVo> vos = list.stream().map(mng->{
            MyVo.ListMngVo vo = new MyVo.ListMngVo();
            vo.setMngId(mng.getId());
            vo.setCommunityId(mng.getCommunityId());
            vo.setPhoneNum(mng.getPhoneNum());
            vo.setRealName(mng.getRealName());
            vo.setUsable(mng.getUsable());
            vo.setIsDelete(mng.getIsDelete());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(mng.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());

        DataGrid<MyVo.ListMngVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**启用禁用管理员*/
    public CommonResult usableMng(MyParam.UsableMngParam param){
        CommunityMng communityMng = communityMngMapper.selectById(param.getMngId());
        if(communityMng==null){
            return Ref.error("错误的管理员编号");
        }
        communityMng.setUsable(param.getUsable());
        communityMngMapper.updateById(communityMng);
        return Ref.ok();
    }

    /**删除恢复管理员*/
    public CommonResult deleteMng(MyParam.DeleteMngParam param){
        CommunityMng communityMng = communityMngMapper.selectById(param.getMngId());
        if(communityMng==null){
            return Ref.error("错误的管理员编号");
        }
        communityMng.setIsDelete(param.getIsDelete());
        communityMngMapper.updateById(communityMng);
        return Ref.ok();
    }

    /**重置管理员密码*/
    public CommonResult resetPass(MyParam.ResetPassParam param){

        CommunityMng communityMng = communityMngMapper.selectById(param.getMngId());
        if(communityMng==null){
            return Ref.error("错误的管理员编号");
        }
        String salt = Md5Util.getSalt();
        String dbPass = Md5Util.makePwd(param.getPassword(), salt);
        communityMng.setSalt(salt);
        communityMng.setPassword(dbPass);
        communityMngMapper.updateById(communityMng);
        return Ref.ok();
    }

    /**添加管理员*/
    public CommonResult addMng(MyParam.AddMngParam param){
        CommunityMng communityMng = communityMngMapper.getByPhoneNum(param.getPhoneNum());
        if(communityMng!=null){
            return Ref.error("该手机号已存在");
        }

        String salt = Md5Util.getSalt();
        String dbPass = Md5Util.makePwd(param.getPassword(), salt);
        Date date = new Date();

        communityMng = new CommunityMng();
        communityMng.setCommunityId(param.getCommunityId());
        communityMng.setPhoneNum(param.getPhoneNum());
        communityMng.setSalt(salt);
        communityMng.setPassword(dbPass);
        communityMng.setRealName(param.getRealName());
        communityMng.setUsable(1);
        communityMng.setIsDelete(0);
        communityMng.setCreateMemberId(1);
        communityMng.setCreateTime(date);
        communityMng.setUpdateMemberId(1);
        communityMng.setUpdateTime(date);
        communityMngMapper.insert(communityMng);
        return Ref.ok();
    }

    /**修改管理员*/
    public  CommonResult modifyMng(MyParam.ModifyMngParam param){
        CommunityMng mng = communityMngMapper.getByPhoneNumAndId(param.getPhoneNum(),
                param.getMngId());
        if(mng!=null){
            return Ref.error("该手机号已存在");
        }
        mng = communityMngMapper.selectById(param.getMngId());
        if(mng==null){
            return Ref.error("错误的管理员编号");
        }
        mng.setPhoneNum(param.getPhoneNum());
        mng.setRealName(param.getRealName());
        communityMngMapper.updateById(mng);
        return Ref.ok();
    }
}
